package cn.xingzhe.monster.user.infrastructure.persistence.converter;

import cn.xingzhe.monster.user.domain.model.UserModel;
import cn.xingzhe.monster.user.infrastructure.persistence.entity.UserEntity;

import java.util.ArrayList;

/**
 * 用户实体转换器
 * 负责 UserEntity 和 UserModel 之间的转换
 *
 * @author System
 * @version 1.0.0
 * @since 2024-01-01
 */
public class UserEntityConverter {

    /**
     * Entity 转 Model
     *
     * @param entity 实体
     * @return 领域模型
     */
    public static UserModel toModel(UserEntity entity) {
        if (entity == null) {
            return null;
        }
        return UserModel.builder()
                .id(entity.getId())
                .username(entity.getUsername())
                .password(entity.getPassword())
                .email(entity.getEmail())
                .phone(entity.getPhone())
                .status(entity.getStatus())
                .roleIds(entity.getRoleIds() != null ? new ArrayList<>(entity.getRoleIds()) : new ArrayList<>())
                .createTime(entity.getCreateTime())
                .updateTime(entity.getUpdateTime())
                .build();
    }

    /**
     * Model 转 Entity
     *
     * @param model 领域模型
     * @return 实体
     */
    public static UserEntity toEntity(UserModel model) {
        if (model == null) {
            return null;
        }
        return UserEntity.builder()
                .id(model.getId())
                .username(model.getUsername())
                .password(model.getPassword())
                .email(model.getEmail())
                .phone(model.getPhone())
                .status(model.getStatus())
                .roleIds(model.getRoleIds() != null ? new ArrayList<>(model.getRoleIds()) : new ArrayList<>())
                .createTime(model.getCreateTime())
                .updateTime(model.getUpdateTime())
                .build();
    }
}

